﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Bon.It.Proxy;
using Bon_It.Model;
using Bonit_It.ViewModel.Base;

namespace Bonit_It.ViewModel
{
    using System.Windows.Data;

    /// <summary>
    /// A Viewmodel for the Userclass.
    /// </summary>
    public class AllUserViewModel : ListViewModel
    {
        private Users _selectedUser;


        public ICommand ShowDetails { get; set; }

        /// <summary>
        /// Filters the specified elements by the username.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        public override bool Filter(object o)
        {
            if (String.IsNullOrEmpty(FilterText))
            {
                return true;
            }
            Users u = o as Users;
            if (u!= null)
            {
                return u.UserName.StartsWith(FilterText, StringComparison.InvariantCultureIgnoreCase);
            }
            return true;
        }

        /// <summary>
        /// The <see cref="BindingList{T}"/> for attaching to the grids.
        /// </summary>
        public ObservableCollection<Users> Source { get; protected set; }

        /// <summary>
        /// Gets or sets the selected user.
        /// </summary>
        /// <value>
        /// The selected user.
        /// </value>
        public Users SelectedUser
        {
            get
            {
                return _selectedUser;
            }
            set
            {
                _selectedUser = value;
                this.OnPropertyChanged("SelectedUser");
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="AllUserViewModel"/> class.
        /// </summary>
        public AllUserViewModel()
        {
            Source = new ObservableCollection<Users>();
            ShowDetails = new RelayCommand(OnShowDetails, CanShowDetails);

            RefreshLookups();
            
            DisplayName = string.Format("Benutzerliste ({0} Datensätze)", Source.Count);
        }

        public override bool CanSave(object argument)
        {
            return false;
        }

        public override bool CanCreate(object argument)
        {
            return true;
        }

        public override bool CanDelete(object argument)
        {
            if (SelectedUser != null)
            {
                return true;
            }
            return false;
        }

        public bool CanShowDetails(object argument)
        {
            return  SelectedUser != null;
        }

        public void OnShowDetails(object argument)
        {
            UserViewModel model = new UserViewModel(SelectedUser);
            OnAttachWorkspace(model);
        }

        public override void OnDelete(object argument)
        {
            if (SelectedUser!= null)
            {
                ServiceProxy.Instance.DeleteUser(SelectedUser);
                Source.Remove(SelectedUser);
            }
        }

       

        public override void OnCreate(object argument)
        {
            UserViewModel model = new UserViewModel(new Users());
            OnAttachWorkspace(model);
        }

        /// <summary>
        /// Reload the data.
        /// </summary>
        public override void RefreshLookups()
        {
            var users = ServiceProxy.Instance.GetUsers().ToList();

            SourceView = CollectionViewSource.GetDefaultView(Source);
            SourceView.Filter = Filter;


            Source.Clear();
            foreach( var user in users)
            {
                this.Source.Add(user);
            }
        }

        public override void CleanUp()
        {
            if (Source != null)
            {
                Source.Clear();
                Source = null;
            }
        }


    }
}

